package rauguelu.programaEventos;

import java.io.IOException;
import java.util.ArrayList;

import rauguelu.items.Evento;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ProgramaEventos extends ListActivity {
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.programa_eventos);
		final TextView sabado = (TextView) findViewById(R.id.diaBusqueda);
		final TextView viernes = (TextView) findViewById(R.id.diaAntes);
		final TextView domingo = (TextView) findViewById(R.id.diaDespues);
		final TextView dsCabeceraAgenda = (TextView) findViewById(R.id.dsCabeceraAgenda);

		
		final String amarillo = "#f2ec36";
		final String verde = "#0a4c42";
		
		Typeface font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
		Typeface font2 = Typeface.createFromAsset(getAssets(), "VSVAB.TTF");
		viernes.setTypeface(font2);
		sabado.setTypeface(font2);
		domingo.setTypeface(font2);
		dsCabeceraAgenda.setTypeface(font);
		
		viernes.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				viernes.setBackgroundColor(Color.parseColor(verde));
				viernes.setTextColor(Color.parseColor(amarillo));
				
				sabado.setBackgroundColor(Color.parseColor(amarillo));
				sabado.setTextColor(Color.parseColor(verde));
				
				domingo.setBackgroundColor(Color.parseColor(amarillo));
				domingo.setTextColor(Color.parseColor(verde));
				
			crearBBDD();
			ArrayList<Evento> Eventos = getItems(viernes.getText().toString());
			setListAdapter(new LibroAdapter(getBaseContext(), R.layout.programa_eventos_item, Eventos));
			}
		});
		
		sabado.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				viernes.setBackgroundColor(Color.parseColor(amarillo));
				viernes.setTextColor(Color.parseColor(verde));
				
				sabado.setBackgroundColor(Color.parseColor(verde));
				sabado.setTextColor(Color.parseColor(amarillo));
				
				domingo.setBackgroundColor(Color.parseColor(amarillo));
				domingo.setTextColor(Color.parseColor(verde));
				
				crearBBDD();
				ArrayList<Evento> Eventos = getItems(sabado.getText().toString());
				setListAdapter(new LibroAdapter(getBaseContext(), R.layout.programa_eventos_item, Eventos));
			}

		});
		
		domingo.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
			
				viernes.setBackgroundColor(Color.parseColor(amarillo));
				viernes.setTextColor(Color.parseColor(verde));
				
				sabado.setBackgroundColor(Color.parseColor(amarillo));
				sabado.setTextColor(Color.parseColor(verde));
				
				domingo.setBackgroundColor(Color.parseColor(verde));
				domingo.setTextColor(Color.parseColor(amarillo));
			
			crearBBDD();
			ArrayList<Evento> Eventos = getItems(domingo.getText().toString());
			setListAdapter(new LibroAdapter(getBaseContext(), R.layout.programa_eventos_item, Eventos));
			}
		});
		
		
		// Creamos la Base de datos
		crearBBDD();
		ArrayList<Evento> Eventos = getItems(viernes.getText().toString());
		
		setListAdapter(new LibroAdapter(this, R.layout.programa_eventos_item, Eventos));
	}

	
	public void onCambiarDia(View v){
		startActivity(new Intent(this, ProgramaEventos.class));
    }
	
	
	public void onVolver(View v){
    	startActivity(new Intent(this, ProgramaEventos.class));
    }
	public ArrayList<Evento> getItems(String sabado) {
		ArrayList<Evento> listaEventos = new ArrayList<Evento>();
				
		miBBDDHelper.abrirBaseDatos();
		if(sabado.equals("Viernes 15")){
			listaEventos = miBBDDHelper.GetEventoDia("15/06/2012");
		}
		if(sabado.equals("Sabado 16")){
			listaEventos = miBBDDHelper.GetEventoDia("16/06/2012");
		}
		if(sabado.equals("Domingo 17")){
			listaEventos = miBBDDHelper.GetEventoDia("17/06/2012");
		}	
		miBBDDHelper.close();
		return listaEventos;
	}
	
	BaseDatosHelper miBBDDHelper;

	public void crearBBDD() {
		miBBDDHelper = new BaseDatosHelper(this);
		try {
			miBBDDHelper.crearDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
	}
	
	private class LibroAdapter extends ArrayAdapter<Evento> {

		private ArrayList<Evento> items;

		public LibroAdapter(Context context, int textViewResourceId,
				ArrayList<Evento> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.programa_eventos_item, null);
			}
			final Evento evento = items.get(position);
			if (evento != null) {
				TextView dsEvento = (TextView) v.findViewById(R.id.dsEvento);
				TextView dsDescripcion = (TextView) v.findViewById(R.id.dsDescripcion);
				TextView dsInfoEvento = (TextView) v.findViewById(R.id.dsInfoEvento);
				TextView horaEvento = (TextView) v.findViewById(R.id.horaEvento);
				TextView dsCategoria = (TextView) v.findViewById(R.id.dsCategoria);
				LinearLayout filaEvento = (LinearLayout) v.findViewById(R.id.filaEvento);
				ImageView imagen_evento= (ImageView) v.findViewById(R.id.detalleItem);
				
				
				if (dsInfoEvento != null) {
					dsInfoEvento.setText(evento.getDsGeo());
				}
				
				if (horaEvento != null) {
					horaEvento.setText(evento.getHoraEvento());
				}
				
				
				if (dsDescripcion != null) {
					dsDescripcion.setText(evento.getDsDescripcion());
				}
				
//				if (horaEvento != null) {
//					horaEvento.setText(evento.getHoraEvento());
//				}
//				if (dsCategoria != null) {
//					dsCategoria.setText(evento.getDsCategoria());
//				}
				
				if (dsEvento != null) {
					dsEvento.setText(evento.getDsEvento());
					Typeface font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
					dsEvento.setTypeface(font);
				}
				if (dsCategoria != null) {
					dsCategoria.setText(evento.getDsCategoria());
					Typeface font = Typeface.createFromAsset(getAssets(), "VSVAB.TTF");
					dsCategoria.setTypeface(font);
				}
				
				imagen_evento.setImageResource(getBaseContext().getResources().getIdentifier(evento.getDsDatosAux()+"", "drawable", getBaseContext().getPackageName()));
				
				filaEvento.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						Bundle bundle = new Bundle();
						bundle.putString("Id",evento.getId());
						bundle.putString("dsEvento",evento.getDsEvento());
						bundle.putString("horaEvento",evento.getHoraEvento());
						bundle.putString("dsMarcado",evento.getDsMarcado());
						bundle.putString("dsDescripcion",evento.getDsDescripcion());
						bundle.putString("cdCategoria",evento.getCdCategoria());
						bundle.putString("dsCategoria",evento.getDsCategoria());
						bundle.putString("diaEvento",evento.getDiaEvento());
						bundle.putString("dsWeb",evento.getDsWeb());
						bundle.putString("dsDatosAux",evento.getDsDatosAux());
						bundle.putString("dsGeo",evento.getDsGeo());
						bundle.putString("claseOrigen","ProgramaEvento");
						
						Intent myIntent = new Intent(view.getContext(), detalleEvento.class);
						myIntent.putExtras(bundle);

						startActivityForResult(myIntent, 0);
					}
				});
					
			}
			return v;
		}
		
	}
}